package com.mp.approvalreq.entity;

import com.mp.approvalreq.service.ReadTemplateServicePrx;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 *   审批导出所需参数
 */
public class ExcelParam {
    private final ReadTemplateServicePrx readService;
    private final User user;
    private final List<Integer> templateIds;
    private final List<String> months;
    private final List<String> emails;
    private final List<String> days;
    private final String exportPath;
    private final String atoken;
    private final String exportType;  // 导出自己或他人申请的 为null则导出全公司的
    private final boolean isInit;     // 判断是否已经初始化
    private String exportTime = null;
    private String reqBody = null;
    private List<User> userList = null;

    public ExcelParam(ReadTemplateServicePrx readService,List<String> days, List<Integer> templateIds, String atoken,
                      List<String> months, List<String> emails, String exportPath, String exportType, User user) {
        this.readService = readService;
        this.user = user;
        this.templateIds = templateIds;
        this.months = months;
        this.emails = emails;
        this.exportPath = exportPath;
        this.atoken = atoken;
        this.exportType = exportType;
        this.days = days;
        isInit = true;
    }

    /**
     * exportType == null : 导出全公司的
     */
    public boolean isExportAllCompany() {
        return exportType == null;
    }

    /**
     *  exportType == 自己contactId : 导出自己的
     */
    public boolean isExportMyApply() {
        if (exportType == null) {
            return false;
        }

        if (exportType.equals("mySelf")) {    // 之前有个版本用了mySelf,兼容一下,之后没有mySelf
            return true;
        }

        if (exportType.equals(String.valueOf(user.getContactId()))) {
            return true;
        }

        return false;
    }

    /**
     *  exportType == xx,xx,xx : 导出其他人的
     */
    public boolean isExportOtherApply() {
        return exportType != null && !isExportMyApply();
    }

    public boolean isExportByMonth() {
        return months != null;
    }

    public boolean isExportByDay() {
        return days != null;
    }

    private void checkParam() {
        if (!isInit) {
            throw new RpcServerLogicalException(" The Excel Param is not init! ");
        }
    }

    public String getExportTime() {
        if (exportTime == null) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(System.currentTimeMillis());
        }
        return exportTime;
    }

    public void setExportTime(String exportTime) {
        this.exportTime = exportTime;
    }

    public ReadTemplateServicePrx getReadService() {
        checkParam();
        return readService;
    }

    public User getUser() {
        checkParam();
        return user;
    }

    public List<Integer> getTemplateIds() {
        checkParam();
        return templateIds;
    }

    public List<String> getMonths() {
        checkParam();
        return months;
    }

    public List<String> getEmails() {
        checkParam();
        return emails;
    }

    public String getExportPath() {
        checkParam();
        return exportPath;
    }

    public String getAtoken() {
        checkParam();
        return atoken;
    }

    public List<String> getDays() {
        checkParam();
        return days;
    }

    public String getReqBody() {
        return reqBody;
    }

    public void setReqBody(String reqBody) {
        this.reqBody = reqBody;
    }

    public String getExportType() {
        return exportType;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
}
